package com.agil.photoalbum.swing.admin;

import com.agil.photoalbum.db.dao.Functions;
import com.agil.photoalbum.db.util.SessionManager;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import java.awt.Color;
import org.hibernate.Session;
 
/**
 *
 * @author  Hovanessyan
 */
public class AdminWorkspace extends javax.swing.JFrame {
    
    private static final Color RED = new java.awt.Color(255, 0, 0);
    private static final Color ORANGE = new java.awt.Color(255, 204, 0);
    private static final Color WHITE = new java.awt.Color(255, 255, 255);
    
    
    /** Creates new form AdminWorkspace */
    public AdminWorkspace() {
        initComponents();
    }
                          
    private void initComponents() {
      
        java.awt.GridBagConstraints gridBagConstraints; 
        
        //initializing all components
        lblUsername = new javax.swing.JLabel();
        jfieldUsername = new javax.swing.JTextField();
        
        lblPassword = new javax.swing.JLabel();
        jfieldPassword = new javax.swing.JPasswordField();
        
        lblInvalidLogin = new javax.swing.JLabel();
        lblAdminMsg = new javax.swing.JLabel();
        
        btnCreate = new javax.swing.JButton();
        btnEdit = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnLogin = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        
        scrollPaneTableUsers = new javax.swing.JScrollPane();
        tableUsers = new javax.swing.JTable();
              
        panelHeader = new javax.swing.JPanel();
        panelWorkspace = new javax.swing.JPanel();
        panelTable = new javax.swing.JPanel();
        panelControls = new javax.swing.JPanel();
        panelLogin = new javax.swing.JPanel();
        panelCentral = new javax.swing.JPanel();
        panelUsername = new javax.swing.JPanel();
        panelPass = new javax.swing.JPanel();      
        panelInvLabel = new javax.swing.JPanel();       
        panelBtn = new javax.swing.JPanel();

        //defining default style elements
        SwingStyles styles = new SwingStyles();
        
        Font defFont = new java.awt.Font("Tahoma", 1, 11);
        
        
        int x = 1024;
        int y = 768;
        Dimension dimMainFrame = new Dimension(x, y);
        
        
        panelWorkspace.setPreferredSize(dimMainFrame);
        panelWorkspace.setLayout(new java.awt.BorderLayout());

        styles.setPanelStyle(panelTable, "Information about users");
        panelTable.setLayout(new javax.swing.BoxLayout(panelTable, javax.swing.BoxLayout.LINE_AXIS));

        UsersTableModel model = new UsersTableModel();
   
        tableUsers.setModel(model);
        
        scrollPaneTableUsers.setViewportView(tableUsers);

        panelTable.add(scrollPaneTableUsers);

        panelWorkspace.add(panelTable, java.awt.BorderLayout.CENTER);
        
        styles.setPanelStyle(panelHeader, "admin controls");
        panelHeader.setLayout(new java.awt.BorderLayout());
        
        lblAdminMsg.setFont(new java.awt.Font("Tahoma", 0, 18));
        lblAdminMsg.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblAdminMsg.setText(jfieldUsername.getText() + "you're now logged in as system administrator!");
        panelHeader.add(lblAdminMsg, java.awt.BorderLayout.WEST);
        
        styles.setBtnStyle(btnExit, "Exit", "click to exit"); 
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                exitActionPerformed(evt);
            }
        });
        
        panelHeader.add(btnExit, java.awt.BorderLayout.EAST);
        
        panelWorkspace.add(panelHeader, java.awt.BorderLayout.PAGE_START);
        
        styles.setPanelStyle(panelControls, "Operations on Users");

        styles.setBtnStyle(btnCreate, "Create", "click to add new user");
        btnCreate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createActionPerformed(evt);
            }
        });
        panelControls.add(btnCreate);

        styles.setBtnStyle(btnEdit, "Edit", "click to edit existing user");
        btnEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editActionPerformed(evt);
            }
        });
        panelControls.add(btnEdit); 

        styles.setBtnStyle(btnDelete, "Delete", "click to delete selected users");
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JOptionPane.showConfirmDialog(rootPane, "Confirm the deletion", "Admin delete dialog", JOptionPane.YES_NO_OPTION);
            }
        });
        panelControls.add(btnDelete);
        
        
        panelWorkspace.add(panelControls, java.awt.BorderLayout.PAGE_END);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("admin controls");
        getContentPane().setBackground(WHITE); 
        setName("adminFrame"); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        //defining border for the login panel
        panelLogin.setPreferredSize(new java.awt.Dimension(300, 300));
        panelLogin.setLayout(new java.awt.BorderLayout());

        panelCentral.setBackground(WHITE);
        panelCentral.setLayout(new java.awt.GridBagLayout());
        
        panelUsername.setBackground(WHITE);
        panelUsername.setMinimumSize(new java.awt.Dimension(200, 19));
        panelUsername.setPreferredSize(new java.awt.Dimension(250, 19));
        panelUsername.setVerifyInputWhenFocusTarget(false);
        panelUsername.setLayout(new java.awt.GridLayout(1, 0, 20, 0));

        lblUsername.setFont(defFont);
        lblUsername.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblUsername.setText("Username");
        panelUsername.add(lblUsername);

       // jfieldUsername.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
      //  jfieldUsername.setMinimumSize(new java.awt.Dimension(70, 19));
      //  jfieldUsername.setPreferredSize(new java.awt.Dimension(70, 19));
        panelUsername.add(jfieldUsername);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(15, 0, 15, 0);
        panelCentral.add(panelUsername, gridBagConstraints);

        panelPass.setBackground(WHITE);
        panelPass.setMinimumSize(new java.awt.Dimension(250, 19));
        panelPass.setPreferredSize(new java.awt.Dimension(250, 19));
        panelPass.setLayout(new java.awt.GridLayout(1, 0, 20, 0));

        lblPassword.setFont(defFont);
        lblPassword.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblPassword.setText("Password");
        panelPass.add(lblPassword);
        panelPass.add(jfieldPassword);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(23, 0, 23, 0);
        panelCentral.add(panelPass, gridBagConstraints);

        panelLogin.add(panelCentral, java.awt.BorderLayout.CENTER);

        panelInvLabel.setBackground(WHITE);
        panelInvLabel.setName("panelInvLabel"); // NOI18N

        lblInvalidLogin.setForeground(WHITE);
        lblInvalidLogin.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblInvalidLogin.setText("Login Failed");
        panelInvLabel.add(lblInvalidLogin);

        panelLogin.add(panelInvLabel, java.awt.BorderLayout.NORTH);

        panelBtn.setBackground(WHITE);
        
        styles.setBtnStyle(btnLogin, "Log in", "click to log in");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loginActionPerformed(evt);
            }
        });
        panelBtn.add(btnLogin);
        
        styles.setPanelStyle(panelLogin, "Log in");
        panelLogin.add(panelBtn, java.awt.BorderLayout.SOUTH);

        getContentPane().add(panelLogin, new java.awt.GridBagConstraints());

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-dimMainFrame.width)/2, (screenSize.height-dimMainFrame.height)/2, dimMainFrame.width, dimMainFrame.height);
    }                       

    private void loginActionPerformed(java.awt.event.ActionEvent evt) {     
            user = jfieldUsername.getText();
            pass = jfieldPassword.getText();
            
            boolean success = Functions.validateAccount(user, pass);
            //boolean isAdmin = Functions.isAdmin(user);
            
            if (success){  
               getContentPane().remove(panelLogin);
               getContentPane().setLayout(new java.awt.BorderLayout());
               getContentPane().add(panelWorkspace, java.awt.BorderLayout.CENTER);
               getContentPane().validate();
               lblInvalidLogin.setForeground(WHITE);
               lblInvalidLogin.repaint(); 
            } else {
                lblInvalidLogin.setForeground(RED);
                lblInvalidLogin.repaint(); 
            }
    }  
    
    private void createActionPerformed(ActionEvent evt) {
        CreateUserDialog crUserDialog = new CreateUserDialog();
        crUserDialog.setLocationRelativeTo(getContentPane());
        crUserDialog.setVisible(true);	 
    }
    
    private void editActionPerformed(ActionEvent evt){
        EditUserDialog edUserDialog = new EditUserDialog();
        edUserDialog.setLocationRelativeTo(getContentPane());
        edUserDialog.setVisible(true);
    }
    
    private void exitActionPerformed(ActionEvent evt){
        Session session = SessionManager.getCurrentSession();
        if(session != null){
            session.close();
        }
        System.exit(0);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AdminWorkspace().setVisible(true);
            }
        });
    }
    
        
    private String user;
    private String pass;
    
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnCreate;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnEdit;
    private javax.swing.JButton btnExit;
    
    private javax.swing.JLabel lblInvalidLogin;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JLabel lblPassword;
    private javax.swing.JLabel lblAdminMsg;
    
    private javax.swing.JPanel panelTable;
    private javax.swing.JPanel panelControls;
    
    private javax.swing.JPasswordField jfieldPassword;
    
    private javax.swing.JScrollPane scrollPaneTableUsers;
    
    private javax.swing.JTable tableUsers;
    
    private javax.swing.JTextField jfieldUsername;
    
    private javax.swing.JPanel panelBtn;
    private javax.swing.JPanel panelCentral;
    private javax.swing.JPanel panelInvLabel;
    private javax.swing.JPanel panelLogin;
    private javax.swing.JPanel panelPass;
    private javax.swing.JPanel panelUsername;
    private javax.swing.JPanel panelWorkspace;
    private javax.swing.JPanel panelHeader;
    // End of variables declaration                   
    
}
